<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h1>消息推送助手</h1>
        <p>请登录您的账户</p>
      </div>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            size="large"
            prefix-icon="User"
            :disabled="loading"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            size="large"
            prefix-icon="Lock"
            :disabled="loading"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" :disabled="loading">
            记住我
          </el-checkbox>
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            :loading="loading"
            @click="handleLogin"
            class="login-button"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <el-divider>
          <span class="divider-text">服务器设置</span>
        </el-divider>
        
        <el-form-item>
          <el-input
            v-model="serverUrl"
            placeholder="服务器地址"
            size="small"
            prefix-icon="Link"
            :disabled="loading"
            @blur="handleServerUrlChange"
          />
        </el-form-item>
        
        <div class="demo-info">
          <el-alert
            title="演示账户"
            type="info"
            :closable="false"
            show-icon
          >
            <template #default>
              <p>用户名: <strong>admin</strong></p>
              <p>密码: <strong>123456</strong></p>
            </template>
          </el-alert>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '../stores/auth'

const authStore = useAuthStore()
const loginFormRef = ref()
const loading = ref(false)
const serverUrl = ref(authStore.serverUrl)

const loginForm = reactive({
  username: '',
  password: '',
  rememberMe: true
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    const valid = await loginFormRef.value.validate()
    if (!valid) return
    
    loading.value = true
    const result = await authStore.login({
      username: loginForm.username,
      password: loginForm.password,
      rememberMe: loginForm.rememberMe
    })
    
    if (result.success) {
      ElMessage.success('登录成功')
    } else {
      ElMessage.error(result.message || '登录失败')
    }
  } catch (error) {
    console.error('登录错误:', error)
    ElMessage.error('登录失败，请重试')
  } finally {
    loading.value = false
  }
}

const handleServerUrlChange = () => {
  if (serverUrl.value !== authStore.serverUrl) {
    authStore.setServerUrl(serverUrl.value)
    ElMessage.success('服务器地址已更新')
  }
}

onMounted(() => {
  // 自动填充演示账户
  loginForm.username = 'admin'
  loginForm.password = '123456'
})
</script>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  padding: 40px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
  
  h1 {
    color: $primary-color;
    margin-bottom: 8px;
    font-size: 28px;
    font-weight: 600;
  }
  
  p {
    color: $text-color-secondary;
    margin: 0;
  }
}

.login-form {
  .el-form-item {
    margin-bottom: 20px;
  }
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
}

.login-footer {
  margin-top: 30px;
  
  .divider-text {
    color: $text-color-secondary;
    font-size: 12px;
  }
  
  .demo-info {
    margin-top: 20px;
    
    :deep(.el-alert__content) {
      p {
        margin: 4px 0;
        font-size: 13px;
        
        strong {
          color: $primary-color;
        }
      }
    }
  }
}
</style>